#include "columnlist.h"

#include "column.h"

ColumnList::~ColumnList()
{
    if(columns != NULL){
        for(unsigned int i=0; i<columns->size(); i++) delete columns->at(i);
        delete columns; columns = NULL;
    }// if
}

ColumnList::ColumnList(const ColumnList& other)
{
    if(other.columns == NULL) this->columns = NULL;
    else{
        columns = new vector<Column *>();
        for(unsigned int i=0; i<other.columns->size(); i++) {
            Column * c = other.columns->at(i)->clone();
            columns->push_back(c);
        }// for
    }// if-else
}

ColumnList::ColumnList(vector<Column *> * columns)
{
    if(columns == NULL) this->columns = NULL;
    else{
        this->columns = new vector<Column *>();
        for(unsigned int i=0; i<columns->size(); i++) {
            Column * c = columns->at(i)->clone();
            this->columns->push_back(c);
        }// for
    }// if-else
}

int ColumnList::size()
{
    return columns->size();
}

void ColumnList::addColumn(Column * column)
{
    if(columns == NULL) columns = new vector<Column *>();
    if(column != NULL) columns->push_back(column->clone());
}

void ColumnList::addColumn(const string & column)
{
    if(columns == NULL) columns = new vector<Column *>();
    Column * c = new Column(column);
    columns->push_back(c);
}

vector<Column *> * ColumnList::getColumns() //const
{
    return columns;
}

string ColumnList::toString()
{
    string str = "<";
    if(columns != NULL){
        for(unsigned int i=0; i<columns->size(); i++){
            str = str + columns->at(i)->toString() + ",";
        }//for
        str.erase(str.length()-1);
    }// if
    str = str + ">";
    return str;
}

void ColumnList::setColumns(vector<Column *> * columns)
{
    for(unsigned int i=0; i<this->columns->size(); i++) delete this->columns->at(i);
    delete this->columns; this->columns = NULL;

    if(columns == NULL) this->columns = NULL;
    else{
        this->columns = new vector<Column *>();
        for(unsigned int i=0; i<columns->size(); i++) {
            Column * c = columns->at(i)->clone();
            this->columns->push_back(c);
        }// for
    }// if-else
}

ColumnList * ColumnList::clone()
{
    return new ColumnList(*this);
}

